<template>
  <div class="login">
    <!-- 背景装饰元素 -->
    <div class="bg-decoration">
      <div class="circle circle-1"></div>
      <div class="circle circle-2"></div>
      <div class="circle circle-3"></div>
      <div class="wave-bg"></div>
    </div>

    <div class="login-container">
      <div class="login-header">
        <div class="logo">
<!--          <div class="logo-icon">🍍</div>-->
          <el-image :src="logo" style="width: 100px;"></el-image>
          <h2>菠萝出行</h2>
        </div>
        <p class="subtitle">欢迎回来，请登录您的账户</p>
      </div>

      <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username" style="margin-bottom: 30px;">
          <el-input
              v-model="loginForm.username"
              type="text"
              size="large"
              auto-complete="off"
              placeholder="请输入手机号"
              class="custom-input"
          >
            <template #prefix>
              <el-icon class="input-icon"><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

<!--        <el-form-item prop="password">-->
<!--          <el-input-->
<!--              v-model="loginForm.password"-->
<!--              type="password"-->
<!--              size="large"-->
<!--              auto-complete="off"-->
<!--              placeholder="请输入密码"-->
<!--              class="custom-input"-->
<!--              @keyup.enter="handleLogin"-->
<!--          >-->
<!--            <template #prefix>-->
<!--              <el-icon class="input-icon"><Lock /></el-icon>-->
<!--            </template>-->
<!--          </el-input>-->
<!--        </el-form-item>-->

        <el-form-item v-if="captchaEnabled" prop="code">
          <div class="code-input-group">
            <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="请输入验证码"
                size="large"
                class="custom-input code-input"
                @keyup.enter="handleLogin"
            >
              <template #prefix>
                <el-icon class="input-icon"><Message /></el-icon>
              </template>
            </el-input>
            <el-button
                @click="getCode"
                :disabled="countdown > 0"
                :class="['code-btn', countdown > 0 ? 'disabled-btn' : '']"
                size="large"
            >
              {{ countdown > 0 ? `${countdown}秒后重试` : '获取验证码' }}
            </el-button>
          </div>
        </el-form-item>

        <div class="form-options">
          <el-checkbox v-model="loginForm.rememberMe" class="remember-me">
            记住密码
          </el-checkbox>
          <!--          <el-link type="primary" class="forgot-link">忘记密码？</el-link>-->
        </div>

        <el-form-item class="login-btn-item">
          <el-button
              :loading="loading"
              size="large"
              type="primary"
              class="login-btn"
              @click.prevent="handleLogin"
          >
            <span v-if="!loading">登录</span>
            <span v-else>登录中...</span>
          </el-button>
        </el-form-item>

        <!--        <div class="register-link" v-if="register">-->
        <!--          <span>还没有账户？</span>-->
        <!--          <router-link class="link-type" :to="'/register'">立即注册</router-link>-->
        <!--        </div>-->
      </el-form>
    </div>

    <!-- 底部版权信息 -->
    <div class="footer">
      <span>Copyright © 2025 菠萝出行 All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, getCurrentInstance } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { User, Lock, Message } from '@element-plus/icons-vue'
import { getCodeImg, getPhoneCode } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from "@/utils/jsencrypt"
import useUserStore from '@/store/modules/user'
import { ElMessage } from "element-plus"
import logo from "@/assets/logo/blcx.png"

const userStore = useUserStore()
const route = useRoute()
const router = useRouter()
const { proxy } = getCurrentInstance()

const title = import.meta.env.VITE_APP_TITLE

const countdown = ref(0)
const timer = ref(null)

const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false,
  code: "",
  uuid: ""
})

const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的手机号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }]
}

const codeUrl = ref("")
const loading = ref(false)
const captchaEnabled = ref(true)
const register = ref(true)
const redirect = ref(undefined)

watch(route, (newRoute) => {
  redirect.value = newRoute.query && newRoute.query.redirect
}, { immediate: true })

function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 })
        Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 })
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 })
      } else {
        Cookies.remove("username")
        Cookies.remove("password")
        Cookies.remove("rememberMe")
      }

      userStore.login(loginForm.value).then(() => {
        const query = route.query
        const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
          if (cur !== "redirect") {
            acc[cur] = query[cur]
          }
          return acc
        }, {})
        router.push({ path: redirect.value || "/", query: otherQueryParams })
      }).catch(() => {
        loading.value = false
      })
    }
  })
}

function getCode() {
  if (countdown.value > 0) return

  const phoneReg = /^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/
  if (!phoneReg.test(loginForm.value.username)) {
    ElMessage.warning('手机号格式必须为11位有效数字！')
    return
  }

  getPhoneCode(loginForm.value.username).then(res => {
    if (res.code == 200) {
      ElMessage.success(res.msg)
      countdown.value = 120
      timer.value = setInterval(() => {
        countdown.value--
        if (countdown.value <= 0) {
          clearInterval(timer.value)
        }
      }, 1000)
    } else {
      ElMessage.error(res.msg)
    }
  })
}

function getCookie() {
  const username = Cookies.get("username")
  const password = Cookies.get("password")
  const rememberMe = Cookies.get("rememberMe")
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  }
}

getCookie()
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  z-index: 0;

  .circle {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    animation: float 6s ease-in-out infinite;
  }

  .circle-1 {
    width: 200px;
    height: 200px;
    top: 10%;
    left: 10%;
    animation-delay: 0s;
  }

  .circle-2 {
    width: 150px;
    height: 150px;
    top: 70%;
    right: 10%;
    animation-delay: 2s;
  }

  .circle-3 {
    width: 100px;
    height: 100px;
    top: 30%;
    right: 30%;
    animation-delay: 4s;
  }

  .wave-bg {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 200px;
    background: linear-gradient(180deg, transparent, rgba(255, 255, 255, 0.05));
    clip-path: polygon(0 60%, 100% 40%, 100% 100%, 0 100%);
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

.login-container {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  box-shadow: 0 25px 45px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 420px;
  z-index: 1;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;

  .logo {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin-bottom: 10px;

    .logo-icon {
      font-size: 32px;
      filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
    }

    h2 {
      margin: 0;
      color: #333;
      font-weight: 600;
      font-size: 24px;
    }
  }

  .subtitle {
    color: #666;
    font-size: 14px;
    margin: 0;
  }
}

.login-form {
  .custom-input {
    margin-bottom: 10px;

    :deep(.el-input__wrapper) {
      border-radius: 12px;
      padding: 0 16px;
      height: 48px;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
      border: 1px solid #e1e8ed;
      transition: all 0.3s ease;

      &:hover {
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
      }

      &.is-focus {
        box-shadow: 0 4px 12px rgba(102, 126, 234, 0.25);
        border-color: #667eea;
      }
    }

    :deep(.el-input__inner) {
      height: 46px;
      font-size: 14px;
    }
  }

  .input-icon {
    color: #999;
    font-size: 16px;
  }

  .code-input-group {
    display: flex;
    gap: 12px;
    align-items: flex-start;

    .code-input {
      flex: 1;
      margin-bottom: 10px;
    }

    .code-btn {
      height: 48px;
      border-radius: 12px;
      background: linear-gradient(135deg, #667eea, #764ba2);
      border: none;
      color: white;
      font-weight: 500;
      transition: all 0.3s ease;
      white-space: nowrap;
      padding: 0 16px;

      &:hover:not(.disabled-btn) {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
      }

      &.disabled-btn {
        background: linear-gradient(135deg, #ccc, #999);
        cursor: not-allowed;
      }
    }
  }

  .form-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;

    .remember-me {
      :deep(.el-checkbox__label) {
        color: #666;
        font-size: 14px;
      }
    }

    .forgot-link {
      font-size: 14px;
      text-decoration: none;

      &:hover {
        text-decoration: underline;
      }
    }
  }

  .login-btn-item {
    margin-bottom: 20px;

    .login-btn {
      width: 100%;
      height: 48px;
      border-radius: 12px;
      background: linear-gradient(135deg, #667eea, #764ba2);
      border: none;
      font-size: 16px;
      font-weight: 600;
      transition: all 0.3s ease;

      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
      }

      &:active {
        transform: translateY(0);
      }
    }
  }

  .register-link {
    text-align: center;
    color: #666;
    font-size: 14px;

    .link-type {
      color: #667eea;
      text-decoration: none;
      font-weight: 500;
      margin-left: 4px;

      &:hover {
        text-decoration: underline;
      }
    }
  }
}

.footer {
  position: fixed;
  bottom: 20px;
  left: 0;
  right: 0;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 12px;
  z-index: 1;
}

// 响应式设计
@media (max-width: 480px) {
  .login-container {
    width: 90%;
    padding: 30px 20px;
    margin: 20px;
  }

  .bg-decoration .circle {
    display: none;
  }
}
</style>
